package cn.dakaqi.services.impl;

import cn.dakaqi.dao.BaseAddressDao;
import cn.dakaqi.entities.ApproveAddress;
import cn.dakaqi.entities.BaseAddress;
import cn.dakaqi.services.ApproveAddressService;
import cn.dakaqi.services.BaseAddressService;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: BaseAddressServiceImpl <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/9/27 11:19
 * @version: 1.0.0
 */
@Component(value = "baseAddressService")
public class BaseAddressServiceImpl implements BaseAddressService
{
    @Autowired
    BaseAddressDao baseAddressDao;
    @Autowired
    ApproveAddressService approveAddressService;

    @Override
    public void saveBaseAddress(BaseAddress baseAddress) throws ServiceRuntimeException
    {

        this.baseAddressDao.save(baseAddress);
        ApproveAddress approveAddress = approveAddressService.findByBaseAddressId(baseAddress.getId());
        if(null == approveAddress || approveAddress.getApproveFirst() >0)
            saveApproveAddress(baseAddress);
    }

    @Override
    public void delById(Long id) throws ServiceRuntimeException
    {
        BaseAddress baseAddress = findOne(id);
        if(null != baseAddress && baseAddress.getVerfierStatus() == 0)
        {
            baseAddress.setDelStatus(DKQConstant.DEL_YES);
            this.baseAddressDao.save(baseAddress);
        }
    }

    @Override
    public void updateBaseAddress(BaseAddress baseAddress) throws ServiceRuntimeException
    {
        this.baseAddressDao.save(baseAddress);
        ApproveAddress approveAddress = approveAddressService.findByBaseAddressId(baseAddress.getId());
        if(null == approveAddress || approveAddress.getApproveFirst() >0)
            saveApproveAddress(baseAddress);
    }

    private void saveApproveAddress(BaseAddress baseAddress)
    {
        ApproveAddress approveAddress = new ApproveAddress(baseAddress.getId());
        approveAddressService.save(approveAddress);
    }
    @Override
    public BaseAddress findByUserId(Long userId) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC,"createTime");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("userId", new SearchFilter("userId", SearchFilter.Operator.EQ, userId));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<BaseAddress> spec = DynamicSpecifications.bySearchFilter(filters.values(), BaseAddress.class);
        List<BaseAddress> list = this.baseAddressDao.findAll(spec,sort);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public List<BaseAddress> findAll() throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC,"createTime");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        filters.put("verfierStatus", new SearchFilter("verfierStatus", SearchFilter.Operator.EQ,2));

        Specification<BaseAddress> spec = DynamicSpecifications.bySearchFilter(filters.values(), BaseAddress.class);
        List<BaseAddress> list = this.baseAddressDao.findAll(spec,sort);
        return list;
    }

    @Override
    public List<BaseAddress> findByAddressLike(String address) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC,"createTime");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        filters.put("verfierStatus", new SearchFilter("verfierStatus", SearchFilter.Operator.EQ,2));
        filters.put("address", new SearchFilter("address", SearchFilter.Operator.LIKE,address));
        Specification<BaseAddress> spec = DynamicSpecifications.bySearchFilter(filters.values(), BaseAddress.class);
        List<BaseAddress> list = this.baseAddressDao.findAll(spec,sort);
        return list;
    }

    @Override
    public BaseAddress findOne(Long baseAddressId) throws ServiceRuntimeException
    {
        return this.baseAddressDao.findOne(baseAddressId);
    }
}
